package com.zswl.decorate.base;

import android.Manifest;
import android.content.Intent;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;

import com.tbruyelle.rxpermissions2.RxPermissions;
import com.zswl.decorate.R;
import com.zswl.decorate.adapter.ImageAdapter;
import com.zswl.decorate.api.ExceptionHandle;
import com.zswl.decorate.bean.ImageBean;
import com.zswl.decorate.util.ImageUtil;
import com.zswl.decorate.util.ToastUtil;
import com.zswl.decorate.widget.SelectPhotoDialog;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.Observer;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public abstract class BasePhotoListActivity extends BackActivity implements ImageAdapter.selectPickListener {
    @BindView(R.id.rv)
    RecyclerView imgRv;
    protected Map<String, String> imgs;//存放要上传图片路径
    private List<ImageBean> dataImg;
    private ImageAdapter adapter;
    private Disposable disposable;

    @Override
    protected void init() {
        imgs = new HashMap<>();
        dataImg = new ArrayList<>();
        dataImg.add(new ImageBean(""));
        adapter = new ImageAdapter(context, dataImg, R.layout.item_imge);
        GridLayoutManager manager = new GridLayoutManager(context, 3);
        imgRv.setLayoutManager(manager);
        imgRv.setAdapter(adapter);
        adapter.setListener(this);
    }

    @Override
    public void photo() {
        changeHeaderImg();
    }


//
//    //    private String takePhotoPath;
//
//    /**
//     * 图片路径
//     *
//     * @param path
//     */
//    public abstract void updateHeadrImg(String path);

    /**
     * 选择头像
     */
    protected void changeHeaderImg() {
        RxPermissions permissions = new RxPermissions(this);
        // Must be done during an initialization phase like onCreate
        permissions
                .request(Manifest.permission.CAMERA,
                        Manifest.permission.WRITE_EXTERNAL_STORAGE,
                        Manifest.permission.READ_EXTERNAL_STORAGE)
                .subscribe(new BaseObserver<Boolean>(context) {
                    @Override
                    public void onError(ExceptionHandle.ResponeThrowable e) {
                        ToastUtil.showShortToast("请打开相机权限");
                    }

                    @Override
                    public void receiveResult(Boolean result) {
                        if (result) {
                            SelectPhotoDialog dialog = new SelectPhotoDialog(context);
                            dialog.setCount(9 - imgs.size());
                            dialog.show();
                        }
                    }
                });

    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);


        if (RESULT_OK == resultCode) {

            //返回图片地址集合：如果你只需要获取图片的地址，可以用这个
//            ArrayList<String> resultPaths = data.getStringArrayListExtra(EasyPhotos.RESULT_PATHS);
            //返回图片地址集合时如果你需要知道用户选择图片时是否选择了原图选项，用如下方法获取
//            boolean selectedOriginal = data.getBooleanExtra(EasyPhotos.RESULT_SELECTED_ORIGINAL, false);
// capture new image
//                updateHeadrImg(takePhotoPath);
//            if (resultPaths != null && resultPaths.size() > 0) {
//                for (String path : resultPaths) {
//                    updateHeadrImg(path);
//                }
//            }
//                    updateHeadrImg(resultPaths.get(0));


        }
    }


    public void updateHeadrImg(final String path) {
        if (imgs.size() < 9) {
            dataImg.add(new ImageBean(path));
            adapter.notifyDataSetChanged();

            //进行压缩
            Observable.just(ImageUtil.getimage(path))
                    .subscribeOn(Schedulers.io()).subscribe(new Observer<String>() {
                @Override
                public void onSubscribe(Disposable d) {
                    disposable = d;
                }

                @Override
                public void onNext(String s) {
                    imgs.put("image" + imgs.size(), s);
                }

                @Override
                public void onError(Throwable e) {

                }

                @Override
                public void onComplete() {

                }
            });

        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (disposable != null)
            disposable.dispose();
    }
}
